<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/Vue.png" alt="" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? '60px':'200px'">

        <!-- 菜单收缩/展开按钮 -->
        <div class="toggle-btn" @click="toggleCollapse">|||</div>

        <!-- 侧边栏菜单区域 default-active="index的值默认选中某个菜单" -->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#313743"
          text-color="#fff"
          active-text-color="#3594F3"
          :unique-opened = "true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
        >

        <!-- 数据渲染的一级菜单 -->
        <el-submenu  v-for="item in menuList" :key="item.id" :index="'/' + item.path">
          <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>

          <!-- 数据渲染二级菜单 -->
           <el-menu-item v-for="sub in item.children" :key="sub.id" :index="'/' + sub.path" @click="saveNavState('/' + sub.path)">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{sub.authName}}</span>
            </template>
          </el-menu-item>

        </el-submenu>



          <!-- 一级菜单 el-submenu -->
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>导航一</span>
            </template>

            <el-menu-item-group>
              <template slot="title">分组一</template>
              <el-menu-item index="1-1">选项1</el-menu-item>
              <el-menu-item index="1-2">选项2</el-menu-item>
            </el-menu-item-group>

            <el-menu-item-group title="分组2">
              <el-menu-item index="1-3">选项3</el-menu-item>
            </el-menu-item-group>

            <el-submenu index="1-4">
              <template slot="title">选项4</template>
              <el-menu-item index="1-4-1">选项1</el-menu-item>
            </el-submenu>
            
          </el-submenu>

          <el-menu-item index="2">
            <i class="el-icon-menu"></i>
            <span slot="title">导航二</span>
          </el-menu-item>

          <el-menu-item index="3" disabled>
            <i class="el-icon-document"></i>
            <span slot="title">导航三</span>
          </el-menu-item>

          <el-menu-item index="4">
            <i class="el-icon-setting"></i>
            <span slot="title">导航四</span>
          </el-menu-item>

        </el-menu>
      </el-aside>

      <!-- 右侧主体内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList:[],
      // 一级菜单图标
      iconsObj :{
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao'
      },
      isCollapse:false,
      // 被激活的连接
      activePath:""
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem("activePath")
  },
  methods: {
    logout() {
      // 清除token
      window.sessionStorage.removeItem('token')
      window.sessionStorage.removeItem('activePath')
      // 编程式跳转
      this.$router.push('/login')
    },
    // 获取菜单数据
    async getMenuList() {
      const {data : res} = await this.$http.get("menus")
      console.log("菜单数据：",res)
      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
      
    },
    // 菜单的收缩/展示
    toggleCollapse() {
        this.isCollapse = !this.isCollapse
    },
    // 保存菜单位置(激活的状态)
    saveNavState(activePath) {
      console.log("当前路由地址：",this.$route.path);
      console.log("activePath:",activePath)
      window.sessionStorage.setItem("activePath",activePath)
      this.activePath = activePath
    }
  },
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    font-size: 20px;
    color: #ffffff;
    display: flex;
    align-items: center;
    img {
      width: 50px;
      height: 50px;
    }
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #313743;
}

.el-main {
  background-color: #e9edf1;
}
.el-menu{
  border: 0;
}
.iconfont{
  margin-right: 8px;
}
.toggle-btn{
  width: 100%;
  height: 25px;
  display: flex;
  color: gainsboro;
  justify-content: center;
  align-items: center;
  background-color: #475163;
  cursor: pointer;
}

</style>